﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using blackhouse.WebNemo.Business.Extensions;

namespace blackhouse.WebNemo.Business.Groups {
    public class ImpGroupRecord : IGroupRecord {

        #region Fields

        private Dictionary<string, object> values = new Dictionary<string, object>();
        private StringCollection updatedColumns = new StringCollection();
        private IGroup group;

        #endregion

        #region Properties

        public string Identyficator { get; set; }

        public int RecordId { get; set; }

        public int BelongsToGroupId { get; set; }

        public DateTime CreatedTime { get; set; }

        public int CreatedBy { get; set; }

        public DateTime LastModifiedTime { get; set; }

        public int LastModifiedBy { get; set; }

        public object this[string column] {
            get { return this.GetValue(column); }
            set { this.values[column] = value; }
        }

        internal IGroup Group {
            get {
                if (this.group == null)
                    this.group = Factory.Instance().GetGroup(this.BelongsToGroupId);
                return this.group;
            }
        }

        #endregion

        #region Methods

        public object GetValue(string column) {
            if (this.values.ContainsKey(column))
                return this.values[column];
            else
                return null;
        }

        public object SetValue(string column, object value) {
            if (!this.Group.Columns[column].AllowUpdateValue)
                throw new UpdateRecordException(2001);
            object current = this.GetValue(column);
            if (current == null && value == null) return current;
            if (current is System.DBNull && value == null) return current;
            if (current == null && value is System.DBNull) return current;
            if (current is System.DBNull && value is System.DBNull) return current;

            if (current != null && current.Equals(value)) return current;

            this.values[column] = value;
            if (!this.updatedColumns.Contains(column))
                this.updatedColumns.Add(column);
            if (current is System.DBNull)
                return null;
            else
                return current;
        }

        public bool IsUpdated(string column) {
            return this.updatedColumns.Contains(column);
        }

        internal bool RecordShouldBeUpdated() {
            return this.updatedColumns.Count > 0;
        }

        public void ChangeIdentyficator(string ident) {
            this.Identyficator = ident;
        }

        public void Remove() {
            throw new NotImplementedException();
        }

        public void Save() {
            throw new NotImplementedException();
        }

        public void Save(int userId) {
            throw new NotImplementedException();
        }

        #endregion

    }
}
